package com.leo.chapter07;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * 通过改写interrupt方法将非标准的取消操作封装在Thread中了
 * @author xuexiaolei
 * @version 2017年10月16日
 */
public class ReaderThread extends Thread {
    private final Socket socket;
    private final InputStream in;

    public ReaderThread(Socket socket, InputStream in) {
        this.socket = socket;
        this.in = in;
    }

    public void interrupt(){
        try {
            socket.close();
        } catch (IOException e) {
            //忽略
        } finally {
            super.interrupt();
        }
    }

    public void run(){
        try {
            byte[] buf = new byte[1024];
            while (true){
                int count = in.read(buf);
                if (count < 0)
                    break;
//                else if (count > 0)
//                    processBuffer(buf, count);
            }
        } catch (IOException e) {
            /* 允许线程退出 */
        }
    }
}
